﻿using Nanolink;
using NanolinkDemo;
using System;
using System.Collections;
using UnityEngine;
using UnityEngine.Events;

namespace CoverShooter
{
    /// <summary>
    /// Takes player input and translates that to commands to CharacterMotor.
    /// </summary>
    [RequireComponent(typeof(CharacterMotor))]
    public class OtherThirdPersonInput : ThirdPersonInput
    {
       
        int playerIndex = -1;

        private void Awake()
        {

        }

        void Start()
        {
            m_Rigidbody = GetComponent<Rigidbody>();
        }

        
        private Rigidbody m_Rigidbody;              // Reference used to move the tank.
        public void OnOtherMove(Vector2 movePos)
        {
            
        }

        public void UpdateMovement(float Horizontal, float Vertical)
        {
            var movement = new CharacterMovement();

            var direction = Horizontal * Vector3.right +
                            Vertical * Vector3.forward;

            var lookAngle = Util.AngleOfVector(_controller.LookTargetInput - _motor.transform.position);

            if (direction.magnitude > float.Epsilon)
                movement.Direction = Quaternion.Euler(0, lookAngle, 0) * direction.normalized;

            if (_controller.ZoomInput)
                movement.Magnitude = 0.5f;
            else
            {
                if (_motor.Gun != null)
                {
                    if (Input.GetButton("Run") && !_motor.IsCrouching)
                        movement.Magnitude = 2.0f;
                    else
                        movement.Magnitude = 1.0f;
                }
                else
                {
                    if (Input.GetButton("Run"))
                        movement.Magnitude = 1.0f;
                    else
                        movement.Magnitude = 0.5f;
                }
            }

            _controller.MovementInput = movement;
        }


        private void Update()
        {
            UpdateTarget();
            //UpdateWeapons();
            UpdateReload();
            //UpdateRolling();
            //UpdateFireAndZoom();
            UpdateGrenade();
            UpdateCrouching();
            UpdateClimbing();
            UpdateCover();
            //UpdateJumping();
            if (isAim == "1")
            {
                _motor.doAim(LookHeight, Vector3.zero, 0);
            }
        }

        public void UpdateMovement(float Magnitude, Vector3 direction)
        {
            var movement = new CharacterMovement();
            
            movement.Direction = direction;
            movement.Magnitude = Magnitude;
           
            _controller.MovementInput = movement;
        }

        public bool initRole(byte playernum, Color color, string name, Vector3 pos, float health)
        {
            m_PlayerNumber = playernum;
            gameObject.name = name;

            return true;
        }
        
        string isAim = "0";
        float LookHeight;
        public void onEvent(Hashtable values)
        {
            string name = (string)values["name"];
            switch (name)
            {
                case "move":
                    //print((string)values["MoveData"]);
                    string[] MoveData = ((string)values["MoveData"]).Split('|');
                    //print(float.Parse(((float)values["Horizontal"]).ToString("f2")) + "===" + float.Parse(((float)values["Vertical"]).ToString("f2")));
                    Vector3 dir = new Vector3(float.Parse(MoveData[1]), float.Parse(MoveData[2]), float.Parse(MoveData[3]));
                    UpdateMovement(float.Parse(MoveData[0]), dir);
                    transform.localEulerAngles = new Vector3(0, float.Parse(MoveData[4]), 0);
                    break;
                case "AimHeight":
                    
                    LookHeight = float.Parse((string)values["AimData"]);
                    break;

                case "Aim":

                    isAim = (string)values["AimData"];
                    break;
                case "Rotation":

                    transform.localEulerAngles = new Vector3(0, float.Parse((string)values["RotData"]), 0);
                    break;
                case "SynLocation":
                    string[] LocData = ((string)values["LocData"]).Split('|');
                    transform.position = new Vector3(float.Parse(LocData[0]), float.Parse(LocData[1]), float.Parse(LocData[2]));
                    break;
                case "Fire":
                    string[] fireData = ((string)values["fireData"]).Split('|');
                    string isFire = fireData[0];
                    if (isFire == "1")
                    {
                        _motor.Gun.RecoiledDirection = new Vector3(float.Parse(fireData[1]), float.Parse(fireData[2]), float.Parse(fireData[3]));
                        _motor.Gun.TempraycastOrigin = new Vector3(float.Parse(fireData[4]), float.Parse(fireData[5]), float.Parse(fireData[6]));
                        _controller.FireInput = true;
                    }
                    else
                    {
                        _controller.FireInput = false;
                    }
                    break;
                case "SwitchWeapon":
                    string WeaponData = (string)values["WeaponData"];
                    _motor.InputWeapon(Convert.ToInt32(WeaponData));
                    break;
                case "SynClimbing":
                    UpdateClimbing();
                    break;
                case "SynJump":
                    print("onEvent=" + (string)values["JumpData"]);
                    UpdateJumping(float.Parse((string)values["JumpData"]));
                    break;
                case "SynRolling":
                    //UpdateRolling((string)values["RollingData"]);
                    string[] RollingData = ((string)values["RollingData"]).Split('|');
                    UpdateRolling(float.Parse(RollingData[0]));
                    transform.localEulerAngles = new Vector3(0, float.Parse(RollingData[1]), 0);
                    transform.position = new Vector3(float.Parse(RollingData[2]), float.Parse(RollingData[3]), float.Parse(RollingData[4]));
                    break;
                default:
                    Debug.Log("未支持的 Player::onEvent 事件: " + name);
                    break;
            }
        }
    }
}